package com.cjw.service.impl.resourceManage;

import com.cjw.mapper.resourceManage.ControllerResourceMapper;
import com.cjw.pojo.dto.database.PageDTO;
import com.cjw.pojo.entity.resourceManage.ControllerResource;
import com.cjw.pojo.query.resourceManage.AddController;
import com.cjw.pojo.query.resourceManage.QueryListController;
import com.cjw.service.resourceManage.ControllerResourceService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author : cjw
 * @date: : 2022/3/6 22:23
 * @description : some
 */
@Service
public class ControllerResourceImpl implements ControllerResourceService {
    @Autowired
    private ControllerResourceMapper resourceMapper;

    @Override
    public PageDTO<List<ControllerResource>> findListController(QueryListController queryListController) {
        PageDTO<List<ControllerResource>> listPageDTO = new PageDTO<>();
        PageHelper.startPage(queryListController.getPage(),queryListController.getSize());
        Example example = new Example(ControllerResource.class);
        Example.Criteria criteria = example.createCriteria();
        Example.Criteria criteria2 = example.createCriteria();
        if (queryListController.getSearch() != null && !queryListController.getSearch().equals("")) {
            criteria2.andLike("name", "%"+queryListController.getSearch()+"%");
            criteria2.orLike("team", "%"+queryListController.getSearch()+"%");
        }
        if (queryListController.getState() != null)
            criteria.andEqualTo("state", queryListController.getState());
        if (queryListController.getMethod() != null)
            criteria.andEqualTo("method", queryListController.getMethod());
        example.setOrderByClause("id desc");
        PageInfo<ControllerResource> controllerResourceInfo = new PageInfo<>(resourceMapper.selectByExample(example));
        listPageDTO.setTotal((int)controllerResourceInfo.getTotal());
        listPageDTO.setDtos(controllerResourceInfo.getList());
        return listPageDTO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addController(AddController addController) {
        ControllerResource controllerResource = new ControllerResource();
        controllerResource.setName(addController.getName());
        controllerResource.setState(1);
        controllerResource.setTeam(addController.getTeam());
        controllerResource.setUrl(addController.getUrl());
        controllerResource.setMethod(addController.getMethod());
        return resourceMapper.insertSelective(controllerResource) == 1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean changeControllerState(Long controllerId, Integer type) {
        ControllerResource controllerResource = new ControllerResource();
        controllerResource.setState(type);
        Example example = new Example(ControllerResource.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", controllerId);
        return resourceMapper.updateByExampleSelective(controllerResource, example) == 1;
    }

    @Override
    public ControllerResource detail(Long id) {
        ControllerResource controllerResource = new ControllerResource();
        controllerResource.setId(id);
        return resourceMapper.selectOne(controllerResource);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateController(AddController addController) {
        ControllerResource controllerResource = new ControllerResource();
        controllerResource.setName(addController.getName());
        controllerResource.setTeam(addController.getTeam());
        controllerResource.setUrl(addController.getUrl());
        controllerResource.setMethod(addController.getMethod());
        Example example = new Example(ControllerResource.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", addController.getId());
        return resourceMapper.updateByExampleSelective(controllerResource, example) == 1;
    }

    @Override
    public Integer getStateByUrl(String url) {
        Example example = new Example(ControllerResource.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andLike("url", "%"+url+"%");
        List<ControllerResource> controllerResources = resourceMapper.selectByExample(example);
        if (controllerResources.size() == 0)
            return 1;
        return controllerResources.get(0).getState();
    }
}
